<?php
namespace app\api\controller;

/**
 * 基本API
 */

class IndexController extends BaseController {

	/**
	 * API信息
	 */
	public function index() {
		$data["title"] = "矿博会电子购票";
		$data["version"] = "0.5.1";
		return $this->toData($data);
	}
	public function test(){
	    $files = $_POST["value"];
	    $tmp = base64_decode($files);
	    $fn = "test.jpg";
	    $fp = './public/uploads/image/'. $fn;

	    $res=file_put_contents( $fp, $tmp);
	    
	    require_once 'extend/aip/AipFace.php';
	    $APP_ID = '11129301';
	    $API_KEY = '3mIAPGGBBHhcYuayPuMU2wk1';
	    $SECRET_KEY = 'F7RxjrOwkZI9f6aeQmQnS3igAC615shT';
	    
	    $client = new \AipFace($APP_ID, $API_KEY, $SECRET_KEY);
		$image = file_get_contents($fp);

		// 调用人脸检测
		$res=$client->detect($image);
		$jsondecode = json_decode($res); //对JSON格式的字符串进行编码
		$result_num=$jsondecode['result_num'];//照片中的人数
		$age=$jsondecode['age'];//年龄
		$face_probability=$jsondecode['face_probability'];//人脸相信度
		$completeness=$jsondecode['completeness'];//人脸完成置信度0表示完整，1表示不完整
		$human=$jsondecode['type']['human'];//是否是活人
		$str="人数".$result_num."年龄".$age."人脸";
		file_put_contents("ctxt", $sn."----".now_time().PHP_EOL, FILE_APPEND);
		if($result_num!=1){
			return json(['code'=>400,'msg'=>'只有存在一个人头像']);
		}
		if($completeness==1){
			return json(['code'=>400,'msg'=>'人脸不全']);
		}
		if($human<=0.5){
			return json(['code'=>400,'msg'=>'人脸不全']);
		}
	    echo $res;exit;
	}
	/**
	 * 更新APP
	 */
	public function update($version) {
		$data['new_version'] = '0.5.1';
		if (apk_verison_compare($data['new_version'], $version) == 1) {
			$data['update'] = 'Yes';
			$data['apk_file_url'] = 'https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk';
			$data['update_log'] = '1，添加删除信用卡接口。\r\n2，添加vip认证。\r\n3，区分自定义消费，一个小时不限制。\r\n4，添加放弃任务接口，小时内不生成。\r\n5，消费任务手动生成。';
			$data['target_size'] = '5M';
			$data['new_md5'] = '295687E756F569C7159974DD493489A5';
			$data['constraint'] = true;
			return json($data);
		} else {
			$data2['update'] = 'No';
			return json($data2);
		}
	}

	/**
	 * 登录，md5：密码加密1加密0不加密
	 */
	public function login($mobile, $password, $md5 = 1) {
		if (request()->isPost()) {
			if (!is_numeric($mobile)) {
				return $this->toError('参数错误');
			}
			$where['mobile'] = $mobile;
			if ($md5) {
				$where['password'] = md5_salt($password);
			} else {
				$where['password'] = $password;
			}
			$model = db('member')->where($where)->find();
			if ($model) {
				if ($model['status'] == 2) {
					return $this->toError('您已经被系统禁用，请联系客服解封');
				} else {
					return $this->toData($model);
				}
			} else {
				return $this->toError('用户不存在或密码错误');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 注册
	 */
	public function regist($mobile, $password, $code, $pid = 0) {
		if (request()->isPost()) {
			if (!is_numeric($mobile) || !is_numeric($code)) {
				return $this->toError('参数错误');
			}
			$data['mobile'] = $mobile;
			$data['password'] = md5_salt($password);
			$validate_result = $this->validate($data, 'Regist');
			if ($validate_result !== true) {
				return $this->toError($validate_result);
			}
			//验证码
			$result = $this->check_code($data['mobile'], $code, '用户注册');
			if ($result != 1) {
				return $this->toError('短信验证码错误!');
			}
			if ($pid) {
				$p_member = db('member')->find($data['pid']);
				if ($p_member) {
					$data['pid'] = $p_member['id'];
				} else {
					return $this->toError('邀请人不存在!');
				}
			}
			$data['add_time'] = now_time();
			$data['update_time'] = now_time();
			$data['login_time'] = now_time();
			$data['login_ip'] = $this->request->ip();
			$result = db('member')->insert($data);
			if ($result) {
				return $this->toSuccess('注册成功');
			} else {
				return $this->toError('注册失败');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	// 找回密码 TODO

	/**
	 * 帮助列表
	 */
	public function help($page) {
		$list = db('sxy')->where('cate_id1', 3)->order('add_time desc')->page($page, 10)->select();
		return $this->toData($list);
	}

	/**
	 * 信息反馈
	 */
	public function feedback($uid, $type, $content) {
		if (request()->isPost()) {
			if (!is_numeric($uid) || !is_numeric($type)) {
				return $this->toError('参数错误');
			}
			$member = db('member')->where('status', 1)->find($uid);
			if (!$member) {
				return $this->toError('用户ID不存在');
			}
			$data['u_id'] = $uid;
			$data['pid'] = $type;
			$data['content'] = $content;
			$data['add_time'] = now_time();
			$result = db('complain')->insert($data);
			if ($result) {
				return $this->toSuccess('反馈成功');
			} else {
				return $this->toError('反馈失败');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信：注册
	 */
	public function regist_code($mobile) {
		if (request()->isPost()) {
			return $this->get_code(config('aliyun'), config('aliyun.regist_id'), $mobile, "用户注册");
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信：忘记密码
	 */
	public function forget_code($mobile) {
		if (request()->isPost()) {
			return $this->get_code(config('aliyun'), config('aliyun.forget_id'), $mobile, "找回密码");
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信
	 * $mobile 手机号
	 * $template_no 模板编号(数据库主键)，注意不是模板ID
	 */
	private function get_code($config, $template_id, $mobile, $message) {
		$code = rand(1000, 9999);
		$data['mobile'] = $mobile;
		$data['code'] = $code;
		$data['message'] = $message;
		$data['add_time'] = now_time();
		include_once ('././extend/smsapi/smsDemo.php');
		$msg = sendSms($config, $template_id, $mobile, 'code', $code);
		if ($msg->Message == "OK") {
			$data['result'] = '发送成功';
			db('verify_code')->insert($data);
			return $this->toData($code);
		} else {
			$err = $msg->Message;
			$data['result'] = '发送失败 - ' . $err;
			db('verify_code')->insert($data);
			return $this->toError('发送失败');
		}
	}

}
